<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/9/5
 * Time: 10:36
 * 用户管理相关接口
 */
class user extends CI_Controller
{


    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');

    }

    //用户筛选接口
    public function search(){

        //搜索条件角色名和性别
        $argc = [];

        if(isset($_GET['sex']) && $_GET['sex'] != ''){
            $argc += [
                'sex' => $_GET['sex']
            ];
        }

        //搜索用户名
        if(isset($_GET['search']) && $_GET['search']){
            $searchArg = [
                'field' => 'nickname',
                'arg'=> $_GET['search'],
                'side' => 'both'
            ];
        }else{
            $searchArg = '';
        }


        if(isset($_GET['role_id']) && $_GET['role_id']){
            $roleSearchArg = [
                'field' => 'role_id',
                'arg'=> $_GET['role_id'],
                'side' => 'both'
            ];
        }else{
            $roleSearchArg = '';
        }

        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        if(!$argc){
            $argc = 0;
        }


//        echo '<pre>';
//        print_r($argc);
//        die;


        if($searchArg){
            $result = $this->user_model->search('*',$argc,$searchArg,$page_num,$start_position);
        }elseif($roleSearchArg){
            $result = $this->user_model->search('*',$argc,$roleSearchArg,$page_num,$start_position);
        }else{
            $result = $this->user_model->page('user',$argc,$page_num,$start_position);
        }



        foreach ($result[0] as $k=>$v){
            $result[0][$k]['createTime'] = date('Y-m-d',$v['createTime']);
        }


        //判断角色是否存在，如果存在就删除不存在的其他数据
//        if(isset($_GET['role_id'])){
//
//            $searchArg2 = [
//                'field' => 'role_id',
//                'arg'=> $_GET['role_id'],
//                'side' => 'both'
//            ];
//            $roleNum = $this->user_model->search('*',0,$searchArg2,$page_num,$start_position);
//
//            if(isset($argc) && $argc['sex'] != ''){
//                //当前角色的总数
//                foreach ($result[0] as $k=>$v){
//                    if(__checkStr($_GET['role_id'],$v['role_id']) == false){
//                        unset($result[0][$k]);
//                    }
//                }
//
////                $result['total_num'] = $roleNum['total_num'];
//                $result['33333333333333333'] = 21333333333;
//
//            }else{
//                $result = $roleNum;
//            }
//
//        }


//        echo json_encode($result,JSON_UNESCAPED_UNICODE);
//        die;

//        sort($result[0]);

        $result['pageNum'] = $page_num;
        $result['this_page'] = $page;

        //搜索





//        echo '<pre>';
//        print_r($result);
//        die;
        echo json_encode($result,JSON_UNESCAPED_UNICODE);

    }


}










































